float4x4 World;
float4x4 View;
float4x4 Projection;

// TODO: add effect parameters here.
texture Texture;
float Alpha = 1;
float4 Color = 1;
float4 FogColor = 0;
float FogStart = 0;
float FogEnd = 1000;
float BlurStart = 100;
float BlurEnd = 300;
float CloseBlurStart = 100;
float CloseBlurEnd = 0;
float4 HighlightColor = float4(1,0,0,1);
float HighlightAlpha = 0.1;
float Bloom = 1;
struct MRT { float4 target0 : COLOR0; float4 target1 : COLOR1;};
struct VertexShaderInput
{
    float4 Position : POSITION0;
	float4 Color : COLOR0;
    float2 TexCoord: TEXCOORD0;
    // TODO: add input channels such as texture
    // coordinates and vertex colors here.
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
	float4 Color : COLOR0;
	float2 TexCoord: TEXCOORD0;
	float4 pos : TEXCOORD1;
    // TODO: add vertex shader outputs such as colors and texture
    // coordinates here. These values will automatically be interpolated
    // over the triangle, and provided as input to your pixel shader.
};
sampler TextureSampler : register(s1) = sampler_state
{
    Texture = (Texture);
	addressU = Clamp;
	addressV = Clamp;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = output.pos = mul(viewPosition, Projection);
	output.TexCoord = input.TexCoord;
	output.Color = input.Color;
    // TODO: add your vertex shader code here.

    return output;
}

MRT PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    // TODO: add your pixel shader code here.
	float4 col = tex2D(TextureSampler, input.TexCoord) * Alpha * Color;
	float fogmul = (input.pos.z - FogStart) / ( FogEnd - FogStart);
	if (fogmul < 0)
	fogmul = 0;
	if (fogmul > 1)
	fogmul = 1;
	MRT mrt = (MRT)0;
	mrt.target0 = col + (((FogColor * col.a) - col) * fogmul);
	//float blurmul = max((input.pos.z - BlurStart) / ( BlurEnd - BlurStart),(input.pos.z - CloseBlurStart) / ( CloseBlurEnd - CloseBlurStart));
	float blurmul = (input.pos.z - BlurStart) / ( BlurEnd - BlurStart);
	if (blurmul < 0)
	blurmul = 0;
	if (blurmul > 1)
	blurmul = 1;
	mrt.target1.r = (blurmul) * col.a; mrt.target1.a = col.a;
	mrt.target1.g = Bloom;
    return mrt;
}
float4 Outline(VertexShaderOutput input) : COLOR0
{
	float2 pos = input.TexCoord;
	float col = 0;
	float div = 0;
	for (float i = -1; i < 1; i+=0.2)
	{
		for (float o = -1; o < 1; o+=0.2)
		{
			col += tex2D(TextureSampler, pos + float2(i * 0.05, o * 0.05)).a;
			div += 1;
		}
	}
	return (HighlightColor * (col) / div) * HighlightAlpha;
}
float4 NoBlurShader(VertexShaderOutput input) : COLOR0
{
    // TODO: add your pixel shader code here.
	float4 col = tex2D(TextureSampler, input.TexCoord) * Alpha * Color;
	float fogmul = (input.pos.z - FogStart) / ( FogEnd - FogStart);
	if (fogmul < 0)
	fogmul = 0;
	if (fogmul > 1)
	fogmul = 1;
	return col + (((FogColor * col.a) - col) * fogmul);
}

technique WithBlur
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
technique WithBlurAndOutline
{
    pass OutlinePass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
	pass OutlinePass2
    {
        // TODO: set renderstates here.
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 Outline();
    }
}
technique NoBlur
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 NoBlurShader();
    }
}
